1 /*
2 Copyright: Marcelo S. N. Mancini (Hipreme|MrcSnm), 2018 - 2021
3 License:   [https://creativecommons.org/licenses/by/4.0/|CC BY-4.0 License].
4 Authors: Marcelo S. N. Mancini
5 
6 	Copyright Marcelo S. N. Mancini 2018 - 2021.
7 Distributed under the CC BY-4.0 License.
8    (See accompanying file LICENSE.txt or copy at
9 	https://creativecommons.org/licenses/by/4.0/
10 */
11 module hip.console.log;
12 import hip.console.console;
13 import hip.util.string;
14 import hip.util.format;
15 
16 ///Creates a variable which is always logged whenever modified.
17 struct Logged(T)
18 {
19     T val;
20     private string name;
21     this(T initial, string name = "")
22     {
23         this.val = initial;
24         this.name = name;
25     }
26 
27     pragma(inline, true) void print(T value, string f = __FILE__, size_t l = __LINE__)
28     {
29         rawlog("Modified ", name, " from ", val, " to ", value, " at ", f, ":", l);
30     }
31 
32     auto opAssign(T value, string f = __FILE__, size_t l = __LINE__)
33     {
34         print(value, f, l);
35         val = value;
36         return this;
37     }
38     auto opOpAssign(string op, T)(T value, string f = __FILE__, size_t l = __LINE__)
39     {
40         T newV = mixin("val",op,"value");
41         print(newV, f, l);
42         val = newV;
43         return this;
44     }
45     bool opEquals(const T other) const{return val == other;}
46     auto opUnary(string op)(string f = __FILE__, size_t l = __LINE__)
47     {
48         static if(op == "++" || op == "--")
49         {
50             T oldV = val;
51             mixin(op,"val;");
52             rawlog("Modified ", name, " from ", oldV, " to ", val, " at ", f, ":", l);
53             return val;
54         }
55         else
56             return mixin(op,"val;");
57     }
58 }
59 private string _formatPrettyFunction(string f) @nogc
60 {
61     import hip.util.string : lastIndexOf;
62 
63     return f[0..f.lastIndexOf("(")];
64 }
65 
66 
67 /**
68 *   hiplog is a special function and should be used only within the engine for documentation.
69 *   It generates less data by not taking the function name and simplifying the read load.
70 *   Think of that as a verbose of what the engine is currently doing.
71 */
72 void hiplog(Args...)(Args a, string file = __FILE__,
73 ulong line = __LINE__)
74 {
75     import hip.config.opts;
76     static if(HIP_TRACK_HIPLOG)
77         Console.DEFAULT.hipLog(BigString("HIP: ", a, " [[", file, ":", line, "]]").toString);
78     else
79         Console.DEFAULT.hipLog(BigString("HIP: ", a).toString);
80 }
81 
82 
83 void logln(Args...)(Args a, string file = __FILE__,
84 string func = __PRETTY_FUNCTION__,
85 ulong line = __LINE__) @nogc
86 {
87 
88     Console.DEFAULT.log(BigString(a, "\n\t\t", file, ":", line, " at ", func._formatPrettyFunction).toString);
89 }
90 
91 
92 void loglnInfo(Args...)(Args a, string file = __FILE__,
93 string func = __PRETTY_FUNCTION__,
94 ulong line = __LINE__)
95 {
96     Console.DEFAULT.info(BigString(a, "\n\t\t", file, ":", line, " at ", func._formatPrettyFunction).toString);
97 }
98 
99 
100 void loglnWarn(Args...)(Args a, string file = __FILE__,
101 string func = __PRETTY_FUNCTION__,
102 ulong line = __LINE__)
103 {
104     Console.DEFAULT.warn(BigString(a, "\n\t\t", file, ":", line, " at ", func._formatPrettyFunction).toString);
105 }
106 
107 
108 void loglnError(Args...)(Args a, string file = __FILE__,
109 string func = __PRETTY_FUNCTION__,
110 ulong line = __LINE__)
111 {
112     Console.DEFAULT.error(BigString(a, "\n\t\t", file, ":", line, " at ", func._formatPrettyFunction).toString);
113 }
114 
115 void rawlog(string msg){Console.DEFAULT.log(msg);}
116 void rawwarn(string msg){Console.DEFAULT.warn(msg);}
117 void rawinfo(string msg){Console.DEFAULT.info(msg);}
118 void rawerror(string msg){Console.DEFAULT.error(msg);}
119 void rawfatal(string msg){Console.DEFAULT.fatal(msg);}